package com.frank.oj.controller.oj;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frank.model.entity.training.TrainingCategory;
import com.frank.oj.annotation.AnonApi;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.model.dto.RegisterTrainingDTO;
import com.frank.oj.model.vo.AccessVO;
import com.frank.oj.model.vo.ProblemVO;
import com.frank.oj.model.vo.TrainingRankVO;
import com.frank.oj.model.vo.TrainingVO;
import com.frank.oj.service.oj.TrainingService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author frank
 * @Date 2024/3/23
 */
@RestController
@RequestMapping("/api")
public class TrainingController {

    @Resource
    private TrainingService trainingService;

    /**
     * 训练题单列表
     *
     * @param limit       单页数
     * @param currentPage 页
     * @param keyword     搜索词
     * @param categoryId  类别
     * @param auth        训练题单权限类型：Public、Private
     * @return 返回训练题单列表
     */
    @GetMapping("/get-training-list")
    @AnonApi
    public CommonResult<IPage<TrainingVO>> getTrainingList(@RequestParam(value = "limit", required = false) Integer limit,
                                                           @RequestParam(value = "currentPage", required = false) Integer currentPage,
                                                           @RequestParam(value = "keyword", required = false) String keyword,
                                                           @RequestParam(value = "categoryId", required = false) Long categoryId,
                                                           @RequestParam(value = "auth", required = false) String auth) {

        return trainingService.getTrainingList(limit, currentPage, keyword, categoryId, auth);
    }

    /**
     * 获取训练分类
     *
     * @return 返回训练分类
     */
    @GetMapping("get-training-category")
    @AnonApi
    public CommonResult<List<TrainingCategory>> getTrainingCategory() {
        return trainingService.getTrainingCategory();

    }

    /**
     * 获取训练详情
     */
    @GetMapping("/get-training-detail")
    @RequiresAuthentication
    public CommonResult<TrainingVO> getTrainingDetail(@RequestParam(value = "tid", required = true) Long tid) {
        return trainingService.getTrainingDetail(tid);
    }

    /**
     * 获取训练题目列表
     */
    @GetMapping("/get-training-problem-list")
    @RequiresAuthentication
    public CommonResult<List<ProblemVO>> getTrainingProblemList(@RequestParam(value = "tid", required = true) Long tid) {
        return trainingService.getTrainingProblemList(tid);
    }

    /**
     * 私有训练权限校验
     */
    @GetMapping("/get-training-access")
    @RequiresAuthentication
    public CommonResult<AccessVO> getTrainingAccess(@RequestParam(value = "tid") Long tid) {
        return trainingService.getTrainingAccess(tid);
    }

    /**
     * 私有训练注册：邀请码校验，输入正确后获取私有训练权限
     */
    @PostMapping("/register-training")
    @RequiresAuthentication
    public CommonResult<Void> toRegisterTraining(@RequestBody RegisterTrainingDTO registerTrainingDto) {
        return trainingService.toRegisterTraining(registerTrainingDto);
    }

    /**
     * 获取训练排行榜
     */
    @GetMapping("/get-training-rank")
    @RequiresAuthentication
    public CommonResult<IPage<TrainingRankVO>> getTrainingRank(@RequestParam(value = "tid", required = true) Long tid,
                                                               @RequestParam(value = "limit", required = false) Integer limit,
                                                               @RequestParam(value = "currentPage", required = false) Integer currentPage,
                                                               @RequestParam(value = "keyword", required = false) String keyword) {
        return trainingService.getTrainingRank(tid, limit, currentPage, keyword);
    }
}
